<?php
namespace Api\Helper
{
/**
 * Autoloading helper for controllers with default method 
 * __auto called from __construct to call all
 * children method with name started from __auto
 * @author Łukasz Torba
 */
 class AutoHelper extends \Api\Helper
 {   
    protected   $m_SharedVars = array();    // Vars created in helper to shared it with controller
     
    /**
     * Calling all autoloading methods     
     */
    public function __auto()
    {
        // loads others initalizers 				
		foreach(get_class_methods($this) as $k => $v)
		{
		  $matched = array();
		  if(preg_match('/__auto(.*)/',$v,$matched))
		  {
			if($matched[1]!='') $this->$matched[0]();							
		  }										
		}
    }
    
    /**
     * Sets shared vars
     * @param Variable name, Variable value
     */
     public function __set($var, $val)
     {
        $this->m_SharedVars[$var] = $val;
     }
     
    /**
     * Return shared vars
     */
     public function __get($var)
     {
        if(array_key_exists($var, $this->m_SharedVars))
            return $this->m_SharedVars[$var];
            
        return parent::__get($var);
     }
     
    /**
     * Return shared vars array
     * @return array
     */
     public function GetSharedVars()
     {
        print_r($this->m_SharedVars);
        return $this->m_SharedVars;
     }
 }
}
?>